package com.jimo.basetools.utils;

import com.jimo.basetools.constants.Constants;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneUtils {

    /**
     * 判断是否为手机号
     * @param phone
     * @return
     */
    public static Boolean isMobile(String phone) {
        String regex = "/^1[3-9]\\d{9}$/";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    /**
     * 生成6位验证码
     * @return 6位验证码
     */
    public static String generateCaptcha() {
        // 随机生成验证码。(6位)
        return generateCaptcha(6);
    }

    /**
     * 随机生成数字字符串
     * @param length 数字字符串长度
     * @return 数字字符串
     */
    public static String generateCaptcha(int length){
        Random random=new Random();
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<length; i++){
            int number = random.nextInt(Constants.NUMERAL.length());
            sb.append(Constants.NUMERAL.charAt(number));
        }
        return sb.toString();
    }
}
